home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / Libraries / DCLAP 6d / dclap6d / DBio / DSeqCmds.cpp < prev    next >
Text File  |  1996-07-05  |  23KB  |  994 lines

  1. // DSeqCmds.cp
  2.  
  3.  
  4. #include <ncbi.h>
  5. #include <DTask.h>
  6. #include <DApplication.h>
  7. #include <DClipboard.h>
  8. #include "DSeqDoc.h"
  9. #include "DSeqEd.h"
  10. #include "DSeqCmds.h"
  11.  
  12.  
  13.  
  14. static Nlm_PoinT gZeroPt = { 0, 0 };
  15.  
  16.  
  17. // DSeqChangeCmd : public DCommand ----------------------------
  18.  
  19. DSeqChangeCmd::DSeqChangeCmd(char* title, DSeqDoc* itsAlnDoc, DView* itsView, DSeqList* itsSeqs) :
  20.     DCommand( cSeqChange, itsView, title, DCommand::kCanUndo, DCommand::kCausesChange)
  21. {
  22.     fOldSeqs= NULL;
  23.     fNewSeqs= NULL;
  24.     fAlnView= itsAlnDoc->fAlnView;
  25.     fSeqDoc= itsAlnDoc;
  26.     //fAlnView->DeinstallEditSeq(); //! prevent mangle do to fBases newhandle 
  27.     
  28.     //- gApplication->CommitLastCommand(); -- Paste seqs is giving probs...
  29.     // theContext:    TCommandHandler;
  30.     //theContext= itsView->GetContext(cSeqChange);
  31.     //ICommand(cSeqChange, theContext, kCanUndo, kCausesChange, theContext);
  32.  
  33. #if 0
  34.         // let user fix fOldSeqs !?
  35.     if (itsSeqs == NULL)  {
  36.         long start, nbases;
  37.         itsAlnDoc->GetSelection( kSeqSel+kMaskSel+kIndexSel+kAllIfNone+kEqualCount, itsSeqs, start, nbases);
  38.         }
  39. #endif
  40.     fOldSeqs= itsSeqs;
  41.     fNewSeqs= new DSeqList(); 
  42.     
  43.     // caller must call Initialize() before other use of this object
  44. }    
  45.  
  46.  
  47. DSeqChangeCmd::~DSeqChangeCmd() 
  48. {
  49.     if (fNewSeqs) {
  50.         fNewSeqs->DeleteAll();         //forget objects -- doesn't affect objects 
  51.         delete fNewSeqs;              
  52.         }
  53.     if (fOldSeqs) {
  54.         //fOldSeqs->FreeAllObjects();     //drop all objects && list !! MEM LEAK, doesn't !
  55.         fOldSeqs->fDeleteObjects= true; // this should avoid leak
  56.         delete fOldSeqs;
  57.         }
  58. }
  59.  
  60. Boolean DSeqChangeCmd::Initialize()  
  61. {
  62.     if (fOldSeqs) {
  63.         short i, n= fOldSeqs->GetSize();
  64.         for (i=0; i<n; i++) {
  65.             DSequence* oldSeq= fOldSeqs->SeqAt(i);
  66.             DSequence* newSeq = ChangeToNew(oldSeq);
  67.             if (newSeq) 
  68.                 fNewSeqs->InsertLast( newSeq);
  69.             else  
  70.                 return false; // fail 
  71.             }
  72.         }
  73.     return true;
  74. }
  75.  
  76. void DSeqChangeCmd::DoIt()  
  77. {
  78.     DCommand::DoIt();
  79.     fSeqDoc->Dirty();
  80.     DoItWork();
  81. }
  82.  
  83. void DSeqChangeCmd::Undo()  
  84. {
  85.     DCommand::Undo();
  86.     fSeqDoc->UnDirty();
  87.     UndoWork();
  88. }
  89.  
  90. void DSeqChangeCmd::Redo()  
  91. {
  92.     DCommand::Redo();
  93.     fSeqDoc->Dirty();
  94.     UndoWork();
  95. }
  96.  
  97. void DSeqChangeCmd::UndoWork()  
  98. {
  99.     DSeqList* tempList= fNewSeqs;
  100.     fNewSeqs= fOldSeqs;
  101.     fOldSeqs= tempList;
  102.     DoItWork();
  103. }
  104.  
  105.  
  106.  
  107. void DSeqChangeCmd::DoItWork()  
  108. {
  109.     short                i, j, nseqs, aFromRow, aToRow, indx, saveEdit;
  110.     DSequence        * oldSeq, * newSeq;
  111.     DSeqList         *    fullList;
  112.     
  113.     if (fOldSeqs && fNewSeqs) {
  114.         saveEdit= fAlnView->fEditRow;        
  115.         aFromRow= kEmptyIndex;
  116.         aToRow= kEmptyIndex;
  117.         nseqs= fNewSeqs->GetSize();
  118.         fullList= fAlnView->fSeqList;
  119.         for (i= 0; i<nseqs; i++) {
  120.             newSeq= fNewSeqs->SeqAt(i);
  121.             oldSeq= fOldSeqs->SeqAt(i);
  122. #if 1
  123.             long k, nfull= fullList->GetSize();
  124.             if (oldSeq) for (k=0, j= kEmptyIndex; k<nfull; k++) {
  125.                 DSequence* aseq= fullList->SeqAt(k);
  126.                 if (aseq && aseq->Checksum() == oldSeq->Checksum() 
  127.                     && aseq->LengthF() == oldSeq->LengthF()
  128.                     && aseq->ModTime() == oldSeq->ModTime()
  129.                     ) {
  130.                         j= k;
  131.                         break;
  132.                         }
  133.                 }
  134. #else
  135.             j= fullList->GetEqualItemNo( oldSeq); // this doesn't work, default equal item tests ptr val
  136. #endif
  137.             if (j>kEmptyIndex) {
  138.                 fullList->AtDelete(j); //fullList->Delete( oldSeq);
  139.                 fullList->InsertBefore( j, newSeq);
  140.                 if (aFromRow == kEmptyIndex) aFromRow= j;
  141.                 else aToRow= j;
  142.                 }    
  143.             DSeqedWindow::UpdateEdWinds( oldSeq, newSeq);
  144.             }
  145.         
  146.         if (aToRow==kEmptyIndex) aToRow= aFromRow;
  147.         
  148.         nseqs= fullList->GetSize();
  149.         for (i=0, indx= 0; i<nseqs; i++) {
  150.             oldSeq= fullList->SeqAt(i);
  151.             if (indx >= aFromRow && indx <= aToRow) {
  152.                 //! also need to shift any selection or deselect all...
  153.                 Nlm_RecT vLoc;
  154.                 fAlnView->GetRowRect( indx, vLoc, 1);
  155.                 fAlnView->InvalRect( vLoc);
  156.                 if (fSeqDoc->fAlnIndex) {
  157.                     fSeqDoc->fAlnIndex->GetRowRect( indx, vLoc, 1);
  158.                     fSeqDoc->fAlnIndex->InvalRect( vLoc);
  159.                     }
  160.                 if (indx == saveEdit) {
  161.                     fAlnView->fEditRow= saveEdit;
  162. #if 0
  163.                     fAlnView->fEditSeq->Locate( vLoc.topLeft, true);
  164.                     vSize= fAlnView->fEditSeq.fSize;
  165.                     fAlnView->fEditSeq->Resize( vSize, false);  
  166. #endif
  167.                     }
  168.                     
  169.                 // ??? isn't this already done above
  170.                 //fAlnView->GetCellRect( indx, 1, vLoc);
  171.                 //fAlnView->InvalRect( vLoc);
  172.                 }
  173.             indx++;
  174.             }
  175.             
  176.         //- fAlnView->SetEmptySelection(kHighlight); 
  177.         //fCommandDone= true; 
  178.         }
  179. }
  180.  
  181. void DSeqChangeCmd::Commit()  
  182. {
  183.     DCommand::Commit(); 
  184.     //this->DoNotification();
  185. }
  186.  
  187.  
  188. DSequence* DSeqChangeCmd::ChangeToNew( DSequence* oldSeq) 
  189. {     
  190.     return (DSequence*) oldSeq->Clone();
  191. }
  192.  
  193. DSequence* DSeqReverseCmd::ChangeToNew( DSequence* oldSeq) 
  194. {     
  195.     return oldSeq->Reverse();
  196. }
  197.  
  198. DSequence* DSeqComplementCmd::ChangeToNew( DSequence* oldSeq) 
  199. {     
  200.     return oldSeq->Complement();
  201. }
  202.  
  203. DSequence* DSeqRevComplCmd::ChangeToNew( DSequence* oldSeq) 
  204. {     
  205.     DSequence* tempSeq= oldSeq->Reverse();  
  206.     DSequence* result= tempSeq->Complement();
  207.     delete tempSeq;  
  208.     return result;
  209. }
  210.  
  211.  
  212. DSequence* DSeqCompressCmd::ChangeToNew( DSequence* oldSeq) 
  213. {     
  214.     return oldSeq->Compress();
  215. }
  216.  
  217. DSequence* DSeqCompressMaskCmd::ChangeToNew( DSequence* oldSeq) 
  218. {     
  219.     return oldSeq->CompressFromMask(fAlnView->fMaskLevel);
  220. }
  221.  
  222. DSequence* DSeqDna2RnaCmd::ChangeToNew( DSequence* oldSeq) 
  223. {     
  224.     return oldSeq->Dna2Rna(TRUE);
  225. }
  226.  
  227. DSequence* DSeqRna2DnaCmd::ChangeToNew( DSequence* oldSeq) 
  228. {     
  229.     return oldSeq->Dna2Rna(FALSE);
  230. }
  231.  
  232. DSequence* DSeqTranslateCmd::ChangeToNew( DSequence* oldSeq) 
  233. {     
  234.     return oldSeq->Translate();
  235. }
  236.  
  237. DSequence* DSeqLockIndelsCmd::ChangeToNew( DSequence* oldSeq) 
  238. {     
  239.     return oldSeq->LockIndels(TRUE);
  240. }
  241.  
  242. DSequence* DSeqUnlockIndelsCmd::ChangeToNew( DSequence* oldSeq) 
  243. {     
  244.     return oldSeq->LockIndels(FALSE);
  245. }
  246.  
  247. DSequence* DSeqUppercaseCmd::ChangeToNew( DSequence* oldSeq) 
  248. {     
  249.     return oldSeq->ChangeCase(TRUE);
  250. }
  251.  
  252. DSequence* DSeqLowercaseCmd::ChangeToNew( DSequence* oldSeq) 
  253. {     
  254.     return oldSeq->ChangeCase(FALSE);
  255. }
  256.  
  257.  
  258.  
  259.  
  260.  
  261.  
  262.         
  263.  
  264.  
  265. // DAlnSlider --------------------------------------
  266.  
  267.  
  268. DAlnSlider::DAlnSlider()
  269. {
  270.     fSlideAll= true;
  271.     fAlnView= NULL;    
  272.     fSeqDoc= NULL;
  273.     fOldSeqs= NULL;
  274.     fNewSeqs= NULL;
  275.     //IAlnSlider( NULL, NULL, 0);    
  276. }
  277.  
  278. DAlnSlider::DAlnSlider( DSeqDoc* itsDoc, DTableView* itsView, DAlnView* itsAlnView,  long oldRC)
  279. {
  280.     IAlnSlider( itsDoc, itsView, itsAlnView, oldRC);    
  281. }
  282.  
  283. DAlnSlider::~DAlnSlider() 
  284. {
  285.     fNewSeqs->fDeleteObjects= false;
  286.     FreeListIfObject( fNewSeqs);  
  287.     fOldSeqs->fDeleteObjects= true;
  288.     FreeListIfObject( fOldSeqs); //drop all objects && list
  289. #if 0
  290.     fOldSelection= Nlm_DestroyRgn( fOldSelection);
  291.     fNewSelection= Nlm_DestroyRgn( fNewSelection);
  292. #endif
  293. }
  294.  
  295. void DAlnSlider::IAlnSlider(DSeqDoc* itsDoc, DTableView* itsView, DAlnView* itsAlnView, long oldRC)
  296. {
  297.     fAlnView= itsAlnView;    
  298.     fSeqDoc= itsDoc;
  299.     fOldSeqs= NULL;
  300.     fNewSeqs= NULL;
  301.     
  302.     fSlideAll= ! (gKeys->shift());
  303.     //fSlideAll=  (gKeys->shift()); // !! which is best !?!?!?
  304.     
  305.  
  306.     IRCshifter( cAlnShiftCmd, itsDoc, itsView, kDoCol, oldRC); 
  307.         // IRCshifter->ITracker->Reset()  calls our Reset() !
  308. #if 0
  309.     fOldSeqs= new DSeqList();  
  310.     fNewSeqs= new DSeqList();  
  311. #if 1
  312.     fOldSelection= fTable->GetSelRect();
  313.     fNewSelection= fOldSelection;
  314. #else
  315.     fOldSelection= Nlm_NewRgn(); 
  316.     fNewSelection= Nlm_NewRgn();  
  317.     CopyRgn( itsAlnView->fSelections, fOldSelection);
  318.     CopyRgn( fOldSelection, fNewSelection);
  319. #endif
  320. #endif
  321. }
  322.  
  323. void DAlnSlider::Reset()
  324. {
  325.     if (fNewSeqs) {
  326.     fNewSeqs->fDeleteObjects= false;
  327.     FreeListIfObject( fNewSeqs);  
  328.     }
  329.     if (fOldSeqs) {
  330.     fOldSeqs->fDeleteObjects= true;
  331.     FreeListIfObject( fOldSeqs); //drop all objects && list
  332.     }
  333.     fOldSeqs= new DSeqList();  
  334.     fNewSeqs= new DSeqList();  
  335.     if (fTable) fOldSelection= fTable->GetSelRect();
  336.     fNewSelection= fOldSelection;
  337. }
  338.  
  339.  
  340.  
  341. void DAlnSlider::TrackFeedback( TrackPhase aTrackPhase,
  342.                     const Nlm_PoinT& anchorPoint, const Nlm_PoinT& previousPoint,
  343.                     const Nlm_PoinT& nextPoint, Nlm_Boolean mouseDidMove, Nlm_Boolean turnItOn)
  344. {
  345.     long row, col, oldrow, oldcol;
  346.     Nlm_RecT     selr, pixr;
  347.     
  348.     switch (aTrackPhase) {
  349.             
  350.         case trackContinue:
  351.             //mouseDidMove= ((previousPoint.x != nextPoint.x)); //(col != pcol);
  352.         case trackBegin:
  353.         case trackEnd:
  354.             if (mouseDidMove) {
  355.                 fTable->PointToCell( nextPoint, row, col);
  356.                 fTable->PointToCell( anchorPoint, oldrow, oldcol);
  357.                 selr= fOldSelection;
  358.                 if (fRowCol == kDoCol)
  359.                     Nlm_OffsetRect( &selr, col - oldcol, 0);
  360.                 else
  361.                     Nlm_OffsetRect( &selr, 0, row - oldrow);
  362. #if 1
  363.                 fTable->GetCellRect( selr, pixr);
  364.                 Nlm_InvertMode();
  365.                 Nlm_FrameRect( &pixr);
  366.                 Nlm_CopyMode();
  367. #else
  368.                 fTable->SelectCells( selr, 
  369.                         !DTableView::kExtend, DTableView::kHighlight, DTableView::kSelect);
  370. #endif
  371.                 }
  372.             break;
  373.         }
  374. }
  375.  
  376.  
  377. void DAlnSlider::DoSlide()
  378. {
  379.     long     row, col, left, right, top, bottom;
  380.     long        start, nbases, dist;
  381.     DSequence * oldSeq, * newSeq;
  382.      
  383.     dist = fNewRC - fOldRC;
  384.     
  385.     // if (fRowCol == kDoCol)
  386.     Nlm_OffsetRect( &fNewSelection, dist, 0);
  387.     
  388.     //fTable->GetFirstSelectedCell( top, left);
  389.     //fTable->GetLastSelectedCell( bottom, right);
  390.     top= fOldSelection.top;
  391.     bottom= fOldSelection.bottom;
  392.     left= fOldSelection.left;
  393.     right= fOldSelection.right;
  394.     for (row= Max(0,top); row<bottom; row++) {        
  395.         col= left;
  396.         //while (col<right && !fTable->IsSelected(row,col)) col++;
  397.         start= col;
  398.         col= right; 
  399.         //while (col<=right && fTable->IsSelected(row,col)) col++;
  400.             // ! nbases == 0 is key to slide all of sequence from sel to top...
  401.         if (dist<0 && fSlideAll) nbases= 0;
  402.         else nbases= col - start;
  403.         if (nbases>=0) {
  404.             oldSeq= fAlnView->fSeqList->SeqAt(row);
  405.             if (oldSeq) {
  406.                 oldSeq->SetIndex( row);
  407.                 oldSeq->SetSelection( start, nbases);
  408.                 newSeq= oldSeq->Slide( dist); //< ?? DSeqChangeCmd !?
  409.                 oldSeq->ClearSelection();        // 12nov92 - fix for write trunc bug? 
  410.                 if (newSeq) {
  411.                     newSeq->ClearSelection();
  412.                     fOldSeqs->InsertLast(oldSeq);
  413.                     fNewSeqs->InsertLast(newSeq);
  414.                   }
  415.                 }
  416.             }
  417.         }
  418. }
  419.  
  420.  
  421. void DAlnSlider::DoItWork()  
  422. {
  423. /* insert "-" at left/5'/bottom of sequence if dist>0
  424.     squeeze out "-" at top/3' of sequence.
  425.     If dist<0 then slide opposite direction.
  426. */
  427.     Nlm_RecT     r;
  428.     long            i, n, zeroshift, left;
  429.     
  430.     if (fMovedOnce) {
  431.         n= fNewSeqs->GetSize();
  432.         for (i=0; i<n; i++) {        
  433.                 // replaceViewSeq
  434.             DSequence* aSeq= fNewSeqs->SeqAt(i);
  435.             fAlnView->fSeqList->AtPut( aSeq->Index(), aSeq);
  436.             fAlnView->GetRowRect( aSeq->Index(), r);
  437.             fAlnView->InvalRect( r);
  438.             if (fSeqDoc->fAlnIndex) {
  439.                 fSeqDoc->fAlnIndex->GetRowRect( aSeq->Index(), r);
  440.                 fSeqDoc->fAlnIndex->InvalRect( r);
  441.                 }
  442.             }
  443.  
  444.         zeroshift= fAlnView->fSeqList->ZeroOrigin(); //!?
  445.         if (zeroshift) {
  446.                 // THIS is getting messy w/ undo/redo because zeroOrigin affects All seqs!
  447.             fAlnView->UpdateAllWidths();
  448.             left= fNewSelection.left; //(*fNewSelection)->rgnBBox.left;
  449.             if (left + zeroshift <= 0)  zeroshift= -left + 1; //??
  450.             Nlm_OffsetRect( &fNewSelection, zeroshift, 0); 
  451.             fAlnView->Invalidate(); 
  452.             }
  453.  
  454.                 // the inval/redraw will handle selection highlight
  455.         fTable->SelectCells( fNewSelection, 
  456.                 DTabSelection::kDontExtend, ! DTabSelection::kHighlight,  DTabSelection::kSelect);
  457.  
  458. #if 1
  459.         n= fNewSeqs->GetSize();
  460.         for (i= 0; i<n; i++)
  461.             DSeqedWindow::UpdateEdWinds( fOldSeqs->SeqAt(i), fNewSeqs->SeqAt(i) );
  462. #endif
  463.         }
  464. }
  465.  
  466.  
  467. void DAlnSlider::UndoWork()  
  468. {
  469.     DSeqList* tmp= fOldSeqs; 
  470.     fOldSeqs= fNewSeqs;
  471.     fNewSeqs= tmp;
  472. #if 1
  473.     Nlm_RecT tmpRect= fOldSelection;
  474.     fOldSelection= fNewSelection;
  475.     fNewSelection= tmpRect;
  476. #else
  477.     Nlm_RgN tmpRgn= fOldSelection;
  478.     fNewSelection= fOldSelection;
  479.     fOldSelection= tmpRgn;
  480. #endif
  481.     DoItWork();
  482. }
  483.  
  484. void DAlnSlider::DoIt()  
  485. {
  486.     long row, col, oldrow, oldcol;
  487.     
  488.     fTable->PointToCell( fNextPoint, row, col);
  489.     fTable->PointToCell( fAnchorPoint, oldrow, oldcol);
  490.     if (fRowCol == kDoRow) { fOldRC= oldrow; fNewRC= row; }
  491.     else if (fRowCol == kDoCol) { fOldRC= oldcol; fNewRC= col; }
  492.     
  493.     if (fNewRC != fOldRC) DoSlide();
  494.      
  495.     DCommand::DoIt();
  496.     fSeqDoc->Dirty();
  497.     DoItWork();
  498. }
  499.  
  500. void DAlnSlider::Undo()  
  501. {
  502.     DCommand::Undo();
  503.     fSeqDoc->UnDirty();
  504.     UndoWork();
  505. }
  506.  
  507. void DAlnSlider::Redo()  
  508. {
  509.     DCommand::Redo();
  510.     fSeqDoc->Dirty();
  511.     UndoWork();
  512. }
  513.  
  514.  
  515.  
  516.  
  517.  
  518.  
  519. // DAlnShifter --------------------------------
  520.  
  521.  
  522.  
  523. DAlnShifter::DAlnShifter()
  524. {
  525. }
  526.  
  527. DAlnShifter::DAlnShifter( DSeqDoc* itsDoc, DTableView* itsView, DAlnView* itsAlnView, long oldRC) :
  528.   DAlnSlider( itsDoc, itsView, itsAlnView, oldRC)
  529. {
  530.     DAlnShifter::IAlnSlider( itsDoc, itsView, itsAlnView, oldRC);
  531. }
  532.  
  533. DAlnShifter::~DAlnShifter() 
  534. {
  535. }
  536.  
  537. void DAlnShifter::IAlnSlider( DSeqDoc* itsDoc, DTableView* itsView, DAlnView* itsAlnView, long oldRC)
  538. {
  539.     fAlnView= itsAlnView;    
  540.     fSeqDoc= itsDoc;
  541.     fOldSeqs= NULL;
  542.     fNewSeqs= NULL;
  543.  
  544.     IRCshifter( cAlnShiftCmd, itsDoc, itsView, kDoRow, oldRC);
  545.     fOldSeqs= new DSeqList();  
  546.     fNewSeqs= new DSeqList();  
  547.     fOldSelection= fTable->GetSelRect();
  548.     fNewSelection= fOldSelection;
  549. }
  550.  
  551.  
  552. void DAlnShifter::TrackFeedback( TrackPhase aTrackPhase,
  553.                     const Nlm_PoinT& anchorPoint, const Nlm_PoinT& previousPoint,
  554.                     const Nlm_PoinT& nextPoint, Nlm_Boolean mouseDidMove, Nlm_Boolean turnItOn)
  555. {
  556.     DAlnSlider::TrackFeedback( aTrackPhase, anchorPoint, previousPoint, nextPoint,
  557.                                                         mouseDidMove, turnItOn);
  558. }
  559.  
  560.  
  561. void DAlnShifter::DoSlide()
  562. {
  563. }
  564.  
  565.  
  566. void DAlnShifter::UndoWork()  
  567. {
  568.     DSeqList* tmp= fOldSeqs; 
  569.     fOldSeqs= fNewSeqs;
  570.     fNewSeqs= tmp;
  571.  
  572.     Nlm_RecT tmpRect= fOldSelection;
  573.     fOldSelection= fNewSelection;
  574.     fNewSelection= tmpRect;
  575.  
  576.     long saverc= fNewRC;
  577.     fNewRC= fOldRC;
  578.     fOldRC= saverc;
  579.     
  580.     DoItWork();
  581. }
  582.  
  583. void DAlnShifter::DoItWork()  
  584. {
  585.     long diff = fNewRC - fOldRC;
  586.     if (fMovedOnce && diff) {
  587.         long i, nrows, newtop, maxrow, top, bottom, minr, maxr;
  588.         DSeqList* tmpseq;
  589.         DSequence* aseq;
  590.         Nlm_RecT    r;
  591.         
  592.         tmpseq= new DSeqList();
  593.         top= fOldSelection.top;
  594.         bottom= fOldSelection.bottom;
  595.         nrows= bottom - top;
  596.         if (nrows<1) nrows= 1;
  597.         newtop= top + diff;
  598.         maxrow= fAlnView->fSeqList->GetSize();
  599.         if (newtop<0) {
  600.             newtop=0;
  601.             diff= newtop - top;
  602.             }
  603.         else if (newtop > maxrow-nrows) {
  604.             newtop= maxrow-nrows;
  605.             diff= newtop - top;
  606.             }
  607.             
  608. #if 1
  609.         fNewSelection= fOldSelection;
  610.         fNewSelection.top= newtop;
  611.         fNewSelection.bottom= newtop + nrows;
  612. #else
  613.         if (fRowCol == kDoCol)
  614.             Nlm_OffsetRect( &fNewSelection, diff, 0);
  615.         else
  616.             Nlm_OffsetRect( &fNewSelection, 0, diff);
  617. #endif            
  618.         
  619.         for ( i= 0; i<nrows; i++) {
  620.           aseq= fAlnView->fSeqList->SeqAt( i+top);
  621.             tmpseq->InsertLast(aseq);
  622.             }
  623.         for (i= nrows-1; i>=0; i--) 
  624.           fAlnView->fSeqList->AtDelete( i+top);
  625.         for (i= 0; i<nrows; i++) {
  626.           aseq= tmpseq->SeqAt(i);
  627.           fAlnView->fSeqList->InsertBefore( i+newtop, aseq);
  628.             }
  629.  
  630.         tmpseq->DeleteAll();  
  631.         delete tmpseq;             //drop list, not objects
  632.          
  633.         minr= Min( top, newtop);
  634.         maxr= Max( top, newtop);
  635.         fTable->GetRowRect( minr, r, (maxr-minr) + nrows);
  636.         fTable->InvalRect( r);
  637.         if (fTable != fAlnView) {
  638.             fAlnView->GetRowRect( minr, r, (maxr-minr) + nrows);
  639.             fAlnView->InvalRect( r);
  640.             }
  641.         
  642.         fTable->SelectCells( fNewSelection, 
  643.                  DTabSelection::kDontExtend, ! DTabSelection::kHighlight,  DTabSelection::kSelect);
  644.         }
  645. }
  646.  
  647.  
  648.  
  649.  
  650.  
  651.  
  652.         
  653.  
  654. //    DAlnEditCommand 
  655.  
  656. DAlnEditCommand::DAlnEditCommand(DSeqDoc* itsDoc, short itsCommand) :
  657.     DCommand( itsCommand, itsDoc->fAlnView, "edit", DCommand::kCanUndo, DCommand::kCausesChange)
  658. {
  659.     fAlnDoc = itsDoc;
  660.     fOldList= NULL;
  661.     //fChangesClipboard = (itsCommand != DApplication::kClear);
  662.     fCausesChange = (itsCommand != DApplication::kCopy);
  663.     fCanUndo = (itsCommand != DApplication::kCopy);  //?
  664.  
  665.     fOldList= new DSeqList();
  666.     long  i, n= fAlnDoc->fSeqList->GetSize();
  667.     for (i=0; i<n; i++) fOldList->InsertLast( fAlnDoc->fSeqList->At(i));
  668.  
  669.     fOldList->fDeleteObjects= false;
  670.     Nlm_LoadRect( &fSelection, 0,0,0,0);
  671.     if (fAlnDoc->fAlnIndex) fSelection= fAlnDoc->fAlnIndex->GetSelRect();
  672. }
  673.  
  674.  
  675.  
  676. DAlnEditCommand::~DAlnEditCommand()  
  677. {
  678.     //fSelection= Nlm_DestroyRgn( fSelection);
  679.     FreeListIfObject( fOldList); 
  680.     fOldList= NULL;
  681. }
  682.  
  683.  
  684. void DAlnEditCommand::CopySelection()
  685. {
  686.     long    irow, minrow, maxrow;
  687.     DSeqList     *    aSeqList;
  688.     DWindow        * clipWindow;
  689.     DAlnView    * clipView = NULL;
  690.     DSeqDoc        *    clipAlnDoc = NULL;
  691.     
  692. #if 1
  693.         // don't want/need DSeqDoc window for clipview !!?
  694.     aSeqList= new DSeqList();
  695.     //clipWindow= new DWindow(0, NULL, DWindow::document, -5, -5, -50, -20, "Clip"); 
  696.     clipWindow= gClipboardMgr->fClipWindow; //!! contents of fClipWindow are getting trashed
  697. #else    
  698.     clipAlnDoc= new DSeqDoc(DSeqDoc::kSeqdoc,NULL,"clip");   
  699.     aSeqList= clipAlnDoc->fSeqList;
  700.     clipWindow= clipAlnDoc;
  701. #endif
  702.  
  703.     minrow= fSelection.top;
  704.     maxrow= fSelection.bottom;
  705.     for (irow=minrow; irow<maxrow; irow++) {
  706.          DSequence* aseq= fAlnDoc->fSeqList->SeqAt(irow);
  707.          if (aseq) aSeqList->InsertLast( aseq->Clone());
  708.         }
  709.  
  710.     clipView= new DAlnView(0,clipWindow,clipAlnDoc,aSeqList, 120, 100);
  711.     if (clipAlnDoc) clipAlnDoc->fAlnView= clipView;
  712.     else clipView->fOwnSeqlist= true;
  713.  
  714. #if 1
  715.     clipView->GetReadyToShow();
  716. #else
  717.     clipView->UpdateSize();
  718.     clipView->UpdateAllWidths(); 
  719. #endif
  720.  
  721.     //this->ClaimClipboard(clipView);  
  722.     gClipboardMgr->SetClipView(clipView);    
  723. }
  724.  
  725.  
  726.  
  727. void DAlnEditCommand::DeleteSelection()
  728.   long irow, minrow, maxrow;
  729.     long totalRows= fAlnDoc->fAlnView->GetMaxRows(); //fOldList->GetSize();
  730.     DAlnIndex* tab= fAlnDoc->fAlnIndex;
  731.   minrow= fSelection.top;
  732.   maxrow= fSelection.bottom;
  733.     Boolean savedel= fAlnDoc->fSeqList->fDeleteObjects;
  734.     fAlnDoc->fSeqList->fDeleteObjects= false;
  735.     for (irow= maxrow-1; irow>=minrow; irow--)  {
  736.         DSequence* aSeq= fAlnDoc->fSeqList->SeqAt(irow);
  737.         if (aSeq) {
  738.             Nlm_RecT     r;
  739.             aSeq->SetDeleted( true);
  740.             fAlnDoc->fSeqList->AtDelete(irow); 
  741.             fAlnDoc->fAlnView->GetRowRect( irow, r, totalRows-irow+1);
  742.             fAlnDoc->fAlnView->InvalRect( r);
  743.             if (tab) {
  744.                 tab->GetRowRect( irow, r, totalRows-irow+1);
  745.                 tab->InvalRect( r);
  746.                 tab->SelectCells( irow, 0, 
  747.                          DTabSelection::kDontExtend, ! DTabSelection::kHighlight, 
  748.                          ! DTabSelection::kSelect);
  749.                 }
  750.             }
  751.         }
  752.     fAlnDoc->fSeqList->fDeleteObjects= savedel;
  753.     fAlnDoc->fAlnView->UpdateSize();
  754. }
  755.  
  756.  
  757. void DAlnEditCommand::RestoreSelection()
  758. {
  759.     long irow, totalRows= fOldList->GetSize();
  760.     long minrow= fSelection.top;
  761.     for (irow=totalRows-1; irow>=0; irow--) {
  762.         DSequence* aSeq= fOldList->SeqAt(irow);
  763.         if (aSeq && aSeq->IsDeleted()) {
  764.             aSeq->SetDeleted( false);
  765.             fAlnDoc->fSeqList->InsertBefore( minrow, aSeq);     
  766.  
  767.             //fAlnDoc->fAlnView->InsRowBefore( minrow, 1, fAlnDoc->fAlnView->fRowHeight);
  768.             //fAlnDoc->fAlnIndex->InsRowBefore( minrow, 1, fAlnDoc->fAlnView-fRowHeight);
  769.             fAlnDoc->fAlnView->ChangeRowSize( fAlnDoc->fAlnView->GetMaxRows(), 1);
  770.             if (fAlnDoc->fAlnIndex) 
  771.                 fAlnDoc->fAlnIndex->ChangeRowSize( fAlnDoc->fAlnIndex->GetMaxRows(), 1);
  772.             }
  773.         }
  774.         
  775.     fAlnDoc->fAlnView->UpdateSize();  
  776.     ReSelect(); 
  777. }
  778.  
  779.  
  780. void DAlnEditCommand::Commit()
  781. {
  782.     long irow, totalRows= fOldList->GetSize();
  783.     Boolean savedel= fOldList->fDeleteObjects;
  784.     fOldList->fDeleteObjects= true;
  785.     for (irow= totalRows-1; irow>=0; irow--) {
  786.         DSequence* aSeq= fOldList->SeqAt(irow);
  787.         if (aSeq && aSeq->IsDeleted())  
  788.             fOldList->AtDelete(irow);
  789.         }
  790.     fOldList->fDeleteObjects= savedel;
  791.     DCommand::Commit(); 
  792. }
  793.  
  794.  
  795. void DAlnEditCommand::ReSelect()
  796. {
  797.     if (fAlnDoc->fAlnIndex)
  798.       fAlnDoc->fAlnIndex->SelectCells( fSelection, 
  799.                  DTabSelection::kDontExtend,  DTabSelection::kHighlight,  DTabSelection::kSelect);
  800. }
  801.  
  802.  
  803. void DAlnEditCommand::DoItWork()
  804. {
  805.     if (fNumber != DApplication::kClear) CopySelection();
  806.     if (fNumber != DApplication::kCopy) DeleteSelection();
  807. }
  808.  
  809.  
  810. void DAlnEditCommand::UndoWork()
  811. {
  812.     if (fNumber != DApplication::kCopy) {
  813.         RestoreSelection();
  814.         //fAlnDoc->fAlnView->ScrollSelectionIntoView(TRUE);
  815.         }
  816. }
  817.  
  818.  
  819. void DAlnEditCommand::RedoWork()
  820. {
  821.     if (fNumber != DApplication::kCopy) {
  822.         DeleteSelection();
  823.         //fAlnDoc->fAlnView->ScrollSelectionIntoView(TRUE);
  824.         }
  825. }
  826.  
  827. void DAlnEditCommand::DoIt()  
  828. {
  829.     DCommand::DoIt();
  830.     fAlnDoc->Dirty();
  831.     DoItWork();
  832. }
  833.  
  834. void DAlnEditCommand::Undo()  
  835. {
  836.     DCommand::Undo();
  837.     fAlnDoc->UnDirty();
  838.     UndoWork();
  839. }
  840.  
  841. void DAlnEditCommand::Redo()  
  842. {
  843.     DCommand::Redo();
  844.     fAlnDoc->Dirty();
  845.     RedoWork();
  846. }
  847.  
  848.  
  849.  
  850.  
  851.  
  852.  
  853.  
  854.  
  855.  
  856.  
  857.  
  858.  
  859. // DAlnPasteCommand 
  860.  
  861.  
  862. DAlnPasteCommand::DAlnPasteCommand(DSeqDoc* itsDoc) :
  863.     DCommand( DApplication::kPaste, itsDoc->fAlnView, "paste", DCommand::kCanUndo, DCommand::kCausesChange)
  864. {
  865.     fAlnDoc = itsDoc;
  866.     fClipList= NULL;
  867.     //fChangesClipboard = false;
  868.  
  869.     long col;
  870.     fInsRow= 0;
  871.     if (fAlnDoc->fAlnIndex) 
  872.         fAlnDoc->fAlnIndex->GetLastSelectedCell( fInsRow, col);
  873.     if (fInsRow ==  DTabSelection::kNoSelection) 
  874.         fInsRow= fAlnDoc->fSeqList->GetSize();
  875.  
  876.     DAlnView* clipview= NULL;
  877.   clipview= (DAlnView*) gClipboardMgr->fClipView; // GetClipView();
  878.     if (clipview && clipview->fKind == DAlnView::kindAlnView) {
  879.         long  i, n;
  880.         fClipList= new DSeqList();
  881.         n= clipview->fSeqList->GetSize();
  882.         for (i=0; i<n; i++) {
  883.             DSequence* aSeq= clipview->fSeqList->SeqAt(i);
  884.             fClipList->InsertLast( aSeq->Clone());
  885.             }
  886.         fClipList->fDeleteObjects= true;
  887.     }
  888. }
  889.  
  890.  
  891. DAlnPasteCommand::~DAlnPasteCommand()  
  892. {
  893.     FreeListIfObject( fClipList); //drop all objects && list 
  894.     fClipList= NULL;
  895. }
  896.  
  897.  
  898. void DAlnPasteCommand::DoItWork()
  899. {
  900.     if (fClipList) {
  901.         long irow, nrows= fClipList->GetSize();
  902.         for (irow= nrows-1; irow>=0; irow--) {
  903.             DSequence* aSeq= fClipList->SeqAt(irow);
  904.             aSeq= (DSequence*) aSeq->Clone();
  905.             aSeq->SetDeleted(true);
  906.             
  907.             fAlnDoc->fSeqList->InsertBefore( fInsRow, aSeq);
  908.             //fAlnDoc->fAlnView->InsRowBefore( fInsRow, 1, fAlnDoc->fAlnView->fRowHeight);
  909.             //fAlnDoc->fAlnIndex->InsRowBefore( fInsRow, 1, fAlnDoc->fAlnView.fRowHeight);
  910.             fAlnDoc->fAlnView->ChangeRowSize( fAlnDoc->fAlnView->GetMaxRows(), 1);
  911.             if (fAlnDoc->fAlnIndex)
  912.                 fAlnDoc->fAlnIndex->ChangeRowSize( fAlnDoc->fAlnIndex->GetMaxRows(), 1);
  913.             }
  914.         fAlnDoc->fAlnView->UpdateAllWidths();
  915.         fAlnDoc->fAlnView->UpdateSize(); 
  916.         } 
  917.  
  918.  
  919. void DAlnPasteCommand::UndoWork()
  920.     if (fClipList) {
  921.         long irow, totalRows= fAlnDoc->fSeqList->GetSize();
  922.         Boolean savedel= fAlnDoc->fSeqList->fDeleteObjects;
  923.         fAlnDoc->fSeqList->fDeleteObjects= true;
  924.         for (irow= totalRows-1; irow>=0; irow--) {
  925.             DSequence* aSeq= fAlnDoc->fSeqList->SeqAt(irow);
  926.             if (aSeq && aSeq->IsDeleted()) {
  927.                 Nlm_RecT     r;
  928.                 aSeq->SetDeleted(false);
  929.                 fAlnDoc->fSeqList->AtDelete(irow);
  930.                 fAlnDoc->fAlnView->GetRowRect( irow, r, totalRows-irow+1);
  931.                 fAlnDoc->fAlnView->InvalRect( r);
  932.                 if (fAlnDoc->fAlnIndex) {
  933.                     fAlnDoc->fAlnIndex->GetRowRect( irow, r, totalRows-irow+1);
  934.                     fAlnDoc->fAlnIndex->InvalRect( r);
  935.                     fAlnDoc->fAlnIndex->SelectCells( irow, 0, 
  936.                              DTabSelection::kDontExtend, ! DTabSelection::kHighlight, ! DTabSelection::kSelect);
  937.                     }
  938.                 }
  939.             }
  940.         fAlnDoc->fSeqList->fDeleteObjects= savedel;
  941.         fAlnDoc->fAlnView->UpdateAllWidths();
  942.         fAlnDoc->fAlnView->UpdateSize();  
  943.         }
  944. }
  945.  
  946.  
  947.  
  948. void DAlnPasteCommand::Commit()  
  949. {
  950.     if (fClipList) {
  951.         long i, n= fAlnDoc->fSeqList->GetSize();
  952.         for (i=0; i<n; i++) {
  953.             DSequence* aseq= fAlnDoc->fSeqList->SeqAt(i);
  954.             aseq->SetDeleted(false);
  955.             }
  956.         }
  957.     DCommand::Commit(); 
  958. }
  959.  
  960.  
  961. void DAlnPasteCommand::DoIt()  
  962. {
  963.     if (fClipList) {
  964.     DCommand::DoIt();
  965.     fAlnDoc->Dirty();
  966.     DoItWork();
  967.     }
  968. }
  969.  
  970. void DAlnPasteCommand::Undo()  
  971. {
  972.     if (fClipList) {
  973.     DCommand::Undo();
  974.     fAlnDoc->UnDirty();
  975.     UndoWork();
  976.     }
  977. }
  978.  
  979. void DAlnPasteCommand::Redo()  
  980. {
  981.     if (fClipList) {
  982.     DCommand::Redo();
  983.     fAlnDoc->Dirty();
  984.     DoItWork();
  985.     }
  986. }
  987.  
  988.  
  989.  
  990.  
  991.